<?php


namespace app\web\controller;


use app\web\service\PrintService;

class PrintUpdate extends Base{
    /**
     * @todo 单个打印：更新快递单打印状态
     * @author hubiao
     */
    public function UpdateExpressState(){
        if($this->request->ispost()){
            $user=request()->user;//登录信息
            $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
            if(empty($shop_id) || !is_numeric($shop_id)){
                return json_ok([],1,'无效的店铺ID');
            }
            $data=$this->request->param();
            if (!isset($data["id"])){
                return json_ok([],1,"未定义id");
            }
            $result=PrintService::updateExpressState($data["id"]);
            return json_ok([],$result["code"],$result["msg"]);
        }
    }
    /**
     * @todo 单个打印：更新发货单打印状态
     * @author hubiao
     */
    public function UpdateInvoiceState(){
        if($this->request->ispost()){
           $user=request()->user;//登录信息
            $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
            if(empty($shop_id) || !is_numeric($shop_id)){
                return json_ok([],1,'无效的店铺ID');
            }
            $data=$this->request->param();
            if (!isset($data["id"])){
                return json_ok([],1,"未定义id");
            }
            $result=PrintService::updateInvoiceState($data["id"]);
            return json_ok([],$result["code"],$result["msg"]);
        }
    }
    /**
     * @todo 单个打印：订单发货
     * @author hubiao
     */
//    public function  OpDeliverySendOrder(){
//        if($this->request->ispost()){
//            $user=request()->user;//登录信息
//            $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
//            $data=$this->request->param();
//            if($data["type"]==1){
//                //无需物流
//                /*
//                 * extBody
//                 * sourceId
//                 * sourceEntryId
//                 * amount
//                 * weight
//                 */
//                $sendGoods = array(
//                    array(
//                        'sourceId' => $data['sourceId'],
//                        'sendGoodEntries' => array(
//                            array(
//                                'sourceEntryId' =>$data['sourceEntryId'],
//                                'amount' => $data['amount'],
//                                'weight' => $data['weight']
//                            )
//                        )
//                    )
//                );
//                $param = array(
//                    'sendGoods' => json_encode($sendGoods),
//                    'extBody' => $data['extBody']
//                );
//                return json(PrintService::opDeliverySendOrderDummy(1,$param));
//            }else if ($data["type"]==2){
//                $sendGoods = array(
//                    array(
//                        'sourceId' => $data['sourceId'],
//                        'sendGoodEntries' => array(
//                            array(
//                                'sourceEntryId' =>$data['sourceEntryId'],
//                                'amount' => $data['amount'],
//                                'weight' => $data['weight']
//                            )
//                        )
//                    )
//                );
//                $param = array(
//                    'sendGoods' => json_encode($sendGoods),
//                    'extBody' => $data['extBody']
//                );
//                return json(PrintService::opDeliverySendOrderOffline(1,$param));
//            }
//        }
//    }
    /**
     * @todo 批量打印：修改备注
     * @author hubiao
     */
    public function OrderMemoAdd(){
        if($this->request->ispost()){
            $user=request()->user;//登录信息
            $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
            $data=$this->request->param();
            $param['orderId'] = $data['orderId'];
            $param['memo']= $data['memo'];
            $param['remarkIcon']= $data['remarkIcon'];
            return json(PrintService::orderMemoAdd($shop_id, $param));
        }
    }
    /**
     * @todo 批量打印：订单详情
     * @author hubiao
     */
    public function GetSellerView(){
        if($this->request->ispost()){
            $user=request()->user;//登录信息
            $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
            $data=$this->request->param();
            $param['orderId'] = $data['orderId'];
            $result = PrintService::getSellerView($shop_id, $param);
            //buyerFeedback
            //sellerMemo
            if (!isset($result['data']['result']['baseInfo']['buyerFeedback'])){
                $result['data']['result']['baseInfo']['buyerFeedback'] = "";
            }
            if (!isset($result['data']['result']['baseInfo']['sellerMemo'])){
                $result['data']['result']['baseInfo']['sellerMemo'] = "";
            }
            return json($result);
        }
    }
    /**
     * @todo 批量打印：更新快递单打印状态
     * @author hubiao
     */
    public function BatchUpdateExpressState(){
        if($this->request->ispost()) {
            $user = request()->user;//登录信息
            $shop_id = empty($user['id']) ? 0 : $user['id'];//店铺ID
            if (empty($shop_id) || !is_numeric($shop_id)) {
                return json_ok([], 1, '无效的店铺ID');
            }
            $data = $this->request->param();
            if (!isset($data["idArray"])){
                return json_ok([],1,"未定义idArray");
            }
            $idArray = explode(",", $data["idArray"]);
            //循环更新状态
            $result = [];
            foreach ($idArray as $value){
                $result=PrintService::updateExpressState($value);
                if($result["code"]==1)
                    return json_ok([],$result["code"],$result["msg"]);
            }
            return json_ok([],$result["code"],$result["msg"]);
        }
    }
    /**
     * @todo 批量打印：更新发货单打印状态
     * @author hubiao
     */
    public function BatchUpdateInvoiceState(){
        if($this->request->ispost()) {
            $user = request()->user;//登录信息
            $shop_id = empty($user['id']) ? 0 : $user['id'];//店铺ID
            if (empty($shop_id) || !is_numeric($shop_id)) {
                return json_ok([], 1, '无效的店铺ID');
            }
            $data = $this->request->param();
            if (!isset($data["idArray"])){
                return json_ok([],1,"未定义idArray");
            }
            $idArray = explode(",", $data["idArray"]);
            //循环更新状态
            $result = [];
            foreach ($idArray as $value){
                $result=PrintService::updateInvoiceState($value);
                if($result["code"]==1)
                    return json_ok([],$result["code"],$result["msg"]);
            }
            return json_ok([],$result["code"],$result["msg"]);
        }
    }
}